package demo.huodongbaoming.config;

import demo.huodongbaoming.loadbalancer.CustomLoadBalancer;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClient;
import org.springframework.cloud.loadbalancer.core.ReactorLoadBalancer;
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

/**
 * 为特定服务配置自定义负载均衡策略
 * 这里为lost-found-service服务配置自定义负载均衡器
 */
@Configuration
@LoadBalancerClient(name = "lost-found-service", configuration = CustomLoadBalancerConfig.class)
public class CustomLoadBalancerConfig {

    /**
     * 为lost-found-service服务配置自定义负载均衡器
     */
    @Bean
    public ReactorLoadBalancer<ServiceInstance> customLoadBalancer(
            Environment environment,
            LoadBalancerClientFactory loadBalancerClientFactory) {
        String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);
        return new CustomLoadBalancer(
                loadBalancerClientFactory.getLazyProvider(name, ServiceInstanceListSupplier.class),
                name);
    }
} 